<?php

/**
 * user_id actions.
 *
 * @package    toolbase
 * @subpackage user_id
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class user_idActions extends ToolbaseActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
        Toolbase::addStat( 'user_id' );
        
        $this->form = new UserIdForm();
        
        $name = $request->getParameter('name');
        
        if( !is_null( $name ) ) {
                
                $this->form->bind(array( 'name' => $request->getParameter('name') ));
            if ($this->form->isValid())
            {
              $this->redirect('user_id_show',
                array(
                        'name' => $name,
                        'wiki' => $request->getParameter('wiki', 'wikipedia'),
                        'lang' => $request->getParameter('lang', 'en'),
                )
              );
            }
        }
        
  }
  
  public function executeShow(sfWebRequest $request)
  {
        
        Toolbase::addStat( 'user_id' );
        
        $db = $this->getDB( $request );
        if( !$db ) $this->redirect( 'user_id/index' );
        
        $result = ToolbaseUserId::getFromName( $request );
        
        if( !$result ) {
        
                sfProjectConfiguration::getActive()->loadHelpers(array('I18N2'));
                
                $this->getUser()->setFlash( 'error', i18n( 'toolbase-error-nouser', $request->getParameter( 'name' ) ) );
                
                $target = 'user_id/index';
                //Fixme: If a user does not exist on a non-enwiki wiki, the form reverts to enwiki
                $this->redirect( $target );
                
        }
        
        $this->user_id = $result['user_id'];
        $this->user_name = $result['user_name'];
        
        $this->form = new UserIdForm();
        
        $this->form->setDefault('name', $request->getParameter('name') );
        
        $this->form->setDefault('lang', $request->getParameter('lang', 'en') );
        $this->form->setDefault('wiki', $request->getParameter('wiki', 'wikipedia') );
        
  }
}
